home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE15 / CPPCLASS / cpp16 / CPPCLU.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-07-17  |  3.0 KB  |  129 lines

  1. unit Cppclu;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.  
  11.   { Mirror of the real C++ class - note the additional method attributes }
  12.   TDLLClass = class
  13.     procedure SetValue(Info : integer);  virtual; abstract;
  14.     function  GetValue : integer;        virtual; abstract;   
  15.     procedure SetEvent(func : TNotifyEvent); virtual; abstract;
  16.   public
  17.     procedure ShowTheValue;   virtual; abstract;
  18.     procedure DoEvent;        virtual; abstract;
  19.  
  20.     property AnEvent : TNotifyEvent   write SetEvent;
  21.     property DLLValue: integer read GetValue write SetValue;
  22.   end;
  23.  
  24.   TForm1 = class(TForm)
  25.        Button1: TButton;
  26.        Button2: TButton;
  27.        Button3: TButton;
  28.        Button4: TButton;
  29.        Button5: TButton;
  30.        Button6: TButton;
  31.        procedure Button1Click(Sender: TObject);
  32.        procedure Button2Click(Sender: TObject);
  33.        procedure Button3Click(Sender: TObject);
  34.        procedure Button4Click(Sender: TObject);
  35.        procedure Button5Click(Sender: TObject);
  36.        procedure Button6Click(Sender: TObject);
  37.  
  38.     private
  39.        procedure DisEnableAllButtons;
  40.  
  41.     public
  42.        procedure bProc(Sender: TObject); export;
  43.        procedure cProc(Sender: TObject); export;
  44.   end;
  45.  
  46.  
  47. var
  48.   Form1: TForm1;
  49.   { Instance of C++ class created in C++ DLL }
  50.   ACppClass : TDLLClass;
  51.  
  52.  
  53. implementation
  54.  
  55. {$R *.DFM}
  56.  
  57. { Make a C++ class }
  58. function  ConstructClass : TDllClass; far; external 'CPPDLL';
  59. procedure DestructClass(DLLClass :TDllClass); far; external 'CPPDLL';
  60.  
  61. procedure TForm1.bProc(Sender: TObject);
  62. begin
  63.   Color   := clRed;
  64.   Caption := 'a GeneralEvent';
  65. end;
  66.  
  67. procedure TForm1.cProc(Sender: TObject);
  68. begin
  69.   Color   := clGreen;
  70.   Caption := 'another GeneralEvent';
  71. end;
  72.  
  73. procedure TForm1.Button1Click(Sender: TObject);
  74. begin
  75.   ACppClass := ConstructClass;
  76.   if ACppClass <> nil then
  77.        DisEnableAllButtons;
  78. end;
  79.  
  80. procedure TForm1.Button2Click(Sender: TObject);
  81. begin
  82.   with ACppClass do
  83.   begin
  84.     { Call methods of C++ object }
  85.     ShowMessage(Format('C++ class Before: %d', [DLLValue]));
  86.     DLLValue := DLLValue + 10;
  87.     ShowMessage(Format('C++ class After: %d', [DLLValue]));
  88.     ShowThevalue;
  89.   end;
  90. end;
  91.  
  92. procedure TForm1.Button3Click(Sender: TObject);
  93. begin
  94.    ACppClass.AnEvent := bProc;
  95. end;
  96.  
  97. procedure TForm1.Button4Click(Sender: TObject);
  98. begin
  99.   ACppClass.AnEvent  := cProc;
  100. end;
  101.  
  102. procedure TForm1.Button5Click(Sender: TObject);
  103. begin
  104.   ACppClass.DoEvent;
  105. end;
  106.  
  107. procedure TForm1.DisEnableAllButtons;
  108. var
  109.   i : integer;
  110. begin
  111.    {Flip all buttons to opposite state}
  112.    for i := 0 to ControlCount-1 do
  113.      if Controls[i] is TButton then
  114.        with TButton(Controls[i]) do
  115.            Enabled := not Enabled;
  116. end;
  117.  
  118. procedure TForm1.Button6Click(Sender: TObject);
  119.  
  120. begin
  121.    DestructClass(ACppClass);
  122.    ACppClass := nil;
  123.     if ACppClass = nil then
  124.         DisEnableAllButtons;
  125.  
  126. end;
  127.  
  128. end.
  129.